package com.dx.mes.service.process.service;

import antlr.StringUtils;
import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.process.service.IFpppackclmService;
import com.dx.mes.facade.process.tx.Fbppackclm.*;


import org.apache.poi.util.StringUtil;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.dx.mes.common.bean.def.GenericDef.*;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;

/**
 * Created by admin on 2017/10/31.
 */
@Service("fpppackclmService")
public class FpppackclmService implements IFpppackclmService {
    private LogUtils logUtils;
    @Autowired
    private IRetWODao retWODao;
    @Autowired
    private IRetPrdInfoDao retPrdInfoDao;
    @Autowired
    private IRetPrdPackDao retPrdPackDao;
    @Autowired
    private IRetBoxDao retBoxDao;
    @Autowired
    private IBisToolDao bisToolDao;
    @Autowired
    private IBisToolGrDao bisToolGrDao;
    @Autowired
    private IBisOpeDao bisOpeDao;
    @Autowired
    private IRetCrtNoDao retCrtNoDao;
    @Autowired
    private IBisMdlDefDao bisMdlDefDao;
    @Autowired
    private IBisMdlGrdDao bisMdlGrdDao;

    public String subMainProc(String evt_no, String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(FpppackclmService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(FpppackclmService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");

        String strOutMsg = null;
        FpppackclmI inTrx = new FpppackclmI();
        FpppackclmO outTrx = new FpppackclmO();

        Session session = retPrdPackDao.getCrSession();

        outTrx.setTrx_id(T_FPPPACKCLM);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
        try {
            inTrx = JacksonUtil.fromJson(strInMsg, FpppackclmI.class);

            char action_flg = inTrx.getAction_flg().charAt(0);
            switch (action_flg) {
                case ACTION_FLG_QUERY:
                    queryByConditionFnc(inTrx, outTrx);
                    break;
                case ACTION_FLG_ADD:
                    addFnc(inTrx, outTrx);
                    break;
                case ACTION_FLG_DELETE:
                    delFnc(inTrx, outTrx);
                    break;
                case ACTION_FLG_CHECK:
                    checkPrdInfo(inTrx, outTrx);
                    break;
                case 'G':
                    getBoxId(inTrx, outTrx);
                    break;
                case 'M':
                    addPrdPackInfo(inTrx, outTrx);
                    break;
                case 'N':
                    queryPrdFnc(inTrx, outTrx);
                    break;
                case 'S':
                    queryAllBoxFnc(inTrx, outTrx);
                    break;
                case 'F':
                    delByBoxFnc(inTrx, outTrx);
                    break;
                case 'L':
                    makeLot(inTrx, outTrx);
                    break;
                case 'O':
                    oneCancelLot(inTrx, outTrx);
                    break;
                case 'W':
                    wholeCancelLot(inTrx, outTrx);
                    break;
                default:
                    outTrx = errActionFnc();
            }
            if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
                session.flush();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception ex) {
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            outTrx.setRtn_mesg("程序执行异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            strOutMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + strOutMsg + "]");
        }
        return strOutMsg;
    }

    public void  addFnc(FpppackclmI inTrx, FpppackclmO outTrx) {

        Timestamp current_timestamp = Util.getCurrentTimestamp();
        List<FpppackclmIA> iaryList = inTrx.getIary();
        String evtUsr = inTrx.getEvt_usr();
        List<FpppackclmOA> orayList = new ArrayList<>();
         if(iaryList.size()>0){
           for(FpppackclmIA fpppackclmIA : iaryList){
               FpppackclmOA  oray = new FpppackclmOA();
               String boxIdFk = fpppackclmIA.getBox_id_fk();
               String hql = "From Ret_prd_pack where box_id_fk ='"+boxIdFk+"'";
               List<Ret_prd_pack> ret_prd_packs = retPrdPackDao.find(hql);
               Ret_box ret_box = new Ret_box();
               ret_box.setBox_id(boxIdFk);
               ret_box.setBox_cate("N");
               ret_box.setReal_emp("N");
               ret_box.setPrd_qty(ret_prd_packs.size());
               ret_box.setPrd_grade_fk(ret_prd_packs.get(0).getPrd_grade());
               ret_box.setMdl_id_fk(ret_prd_packs.get(0).getMdl_id_fk());
               ret_box.setWo_id_fk(ret_prd_packs.get(0).getWo_id_fk());
               ret_box.setBox_stat("COMP");
               ret_box.setEvt_timestamp(current_timestamp);
               ret_box.setEvt_usr(evtUsr);
               Ret_prd_info ret_prd_info1 = retPrdInfoDao.get(ret_prd_packs.get(0).getPrd_seq_id());
               ret_box.setBatch_no_fk(ret_prd_info1.getBatch_no());
               retBoxDao.save(ret_box);
               int i=1;//用于生成slot_no
               for(Ret_prd_pack ret_prd_pack : ret_prd_packs){
                   Ret_prd_info ret_prd_info = retPrdInfoDao.get(ret_prd_pack.getPrd_seq_id());
                  // ret_prd_info.setPack_no(ret_prd_pack.getBox_id_fk());
                   ret_prd_info.setBox_id_fk(ret_prd_pack.getBox_id_fk());
                   ret_prd_info.setPrd_stat("COMP");
                   ret_prd_info.setEvt_timestamp(current_timestamp);
                   ret_prd_info.setEvt_usr(evtUsr);
                   ret_prd_info.setSlot_no(String.valueOf(i));
                   //出账操作
                   ret_prd_info.setPv_ope_id_fk(_SPACE);
                   ret_prd_info.setPv_ope_no_fk(_SPACE);
                   ret_prd_info.setPv_ope_ver_fk(_SPACE);
                   ret_prd_info.setPv_path_id_fk(_SPACE);
                   ret_prd_info.setPv_path_ver_fk(_SPACE);
                   ret_prd_info.setPv_tool_id_fk(_SPACE);
                   ret_prd_info.setPv_toolg_id_fk(_SPACE);
                   ret_prd_info.setPv_proc_id_fk(_SPACE);
                   ret_prd_info.setPv_tool_run_mode(_SPACE);
                   ret_prd_info.setLogof_usr(evtUsr);
                   ret_prd_info.setLogof_timestamp(current_timestamp);
                   ret_prd_info.setEvt_cate(EVT_INNER_BOX_PACK);

                   retPrdInfoDao.update(ret_prd_info);
                   retPrdPackDao.delete(ret_prd_pack);
                   i=i+1;
               }
               Ret_wo ret_wo = retWODao.get(ret_prd_packs.get(0).getWo_id_fk());
               if(null!=ret_wo){
                   ret_wo.setComp_prd_qty(ret_wo.getComp_prd_qty()+ret_prd_packs.size());
                   ret_wo.setEvt_timestamp(current_timestamp);
                   ret_wo.setEvt_usr(evtUsr);
                   retWODao.update(ret_wo);
               }
               Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_box.getMdl_id_fk());
               oray.setMdl_id_fk(ret_box.getMdl_id_fk());
               oray.setBox_id_fk(ret_box.getBox_id());
               oray.setPrd_qty(ret_box.getPrd_qty());
               oray.setMdl_desc(bis_mdl_def.getMdl_dsc());
               oray.setWo_id_fk(ret_box.getWo_id_fk());
               orayList.add(oray);
           }
         }else {
             outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
             outTrx.setRtn_mesg("出账箱子个数为0");
             return;
         }
        outTrx.setOary(orayList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    public void delFnc(FpppackclmI inTrx, FpppackclmO outTrx) {
        FpppackclmIA iary = inTrx.getIary().get(0);
        String prdSeqId = iary.getPrd_seq_id();
        Ret_prd_pack ret_prd_pack = retPrdPackDao.get(prdSeqId);
        Ret_prd_info ret_prd_info = retPrdInfoDao.get(prdSeqId);
        if (ret_prd_pack == null && ret_prd_info.getPack_no() != null) {
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]已经包装出账，箱号为[" + ret_prd_info.getPack_no() + "]");
            return;
        } else if (ret_prd_pack == null && ret_prd_info.getPack_no() == null) {
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]没有进行包装操作，无法取消");
            return;
        }
        retPrdPackDao.delete(ret_prd_pack);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    public void queryByConditionFnc(FpppackclmI inTrx, FpppackclmO outTrx) {

        FpppackclmIA iary = inTrx.getIary().get(0);
        String prdSeqId = iary.getPrd_seq_id();
        Ret_prd_pack ret_prd_pack = retPrdPackDao.get(prdSeqId);
        if (ret_prd_pack == null) {
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]不在包装内");
            return;
        }
        String hql = "From Ret_prd_pack where box_id_fk ='" + ret_prd_pack.getBox_id_fk() + "'";
        List<Ret_prd_pack> ret_prd_packs = retPrdPackDao.find(hql);
        List<FpppackclmOB> oaryList = new ArrayList<FpppackclmOB>();
        FpppackclmOB fpppackclmOB = new FpppackclmOB();
        fpppackclmOB.setBox_id_fk(ret_prd_pack.getBox_id_fk());
        fpppackclmOB.setMdl_id_fk(ret_prd_pack.getMdl_id_fk());
        fpppackclmOB.setPrd_grade(ret_prd_pack.getPrd_grade());
        fpppackclmOB.setPrd_qty(ret_prd_packs.size());
        oaryList.add(fpppackclmOB);
        outTrx.setOaryB(oaryList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);

    }

    public void queryPrdFnc(FpppackclmI inTrx, FpppackclmO outTrx) {

        FpppackclmIA iary = inTrx.getIary().get(0);
        String boxIdFk = iary.getBox_id_fk();
        String hql = "From Ret_prd_pack where box_id_fk ='" + boxIdFk + "'";
        List<Ret_prd_pack> ret_prd_packs = retPrdPackDao.find(hql);
        List<FpppackclmOA> oaryAList = new ArrayList<FpppackclmOA>();
        for (Ret_prd_pack ret_prd_pack1 : ret_prd_packs) {
            FpppackclmOA fpppackclmOA = new FpppackclmOA();
            fpppackclmOA.setPrd_seq_id(ret_prd_pack1.getPrd_seq_id());
            fpppackclmOA.setMdl_id_fk(ret_prd_pack1.getMdl_id_fk());
            fpppackclmOA.setPrd_grade(ret_prd_pack1.getPrd_grade());
            oaryAList.add(fpppackclmOA);
        }
        outTrx.setOary(oaryAList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    public void checkPrdInfo(FpppackclmI inTrx, FpppackclmO outTrx) {
             FpppackclmIA iary = inTrx.getIary().get(0);
             String prdSeqId = iary.getPrd_seq_id();
             String toolIdFk = iary.getTool_id_fk();
             String wo_id_fk = iary.getWo_id_fk();
             Boolean boxFlag = false;

            Ret_prd_info ret_prd_info = retPrdInfoDao.get(prdSeqId);
            if (ret_prd_info == null) {
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]不存在");
                return;
            }
            Bis_tool bis_tool = bisToolDao.get(toolIdFk);
            if (bis_tool == null) {
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("设备代码[" + toolIdFk + "]不存在");
                return;
            }
            if (PROC_COMPLETE.equals(ret_prd_info.getPrd_stat())) {
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]已经包装完成");
                return;
            }
            if (PROC_SHIP.equals(ret_prd_info.getPrd_stat())) {
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("屏体代码[" + prdSeqId + "]已经入库");
                return;
            }
            if (PROC_SCRP.equals(ret_prd_info.getPrd_stat())) {
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("屏体代码[" + prdSeqId + "]已经被报废");
                return;
            }
            if (PROC_HOLD.equals(ret_prd_info.getPrd_stat())) {
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("屏体代码[" + prdSeqId + "]已经被HOLD");
                return;
            }
            if (BANK_IN.equals(ret_prd_info.getBnk_flg())) {
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("该屏体在在制仓内，无法包装");
                return;
            }
            if (PROC_WAIT.equals(ret_prd_info.getPrd_stat())) {
                String hqlm = "from Bis_tool_gr where tool_id_fk = '" + toolIdFk + "'";
                List<Bis_tool_gr> listGr = bisToolGrDao.find(hqlm);
                if (listGr.size() == 0) {
                    outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                    outTrx.setRtn_mesg("设备代码[" + toolIdFk + "]群组信息不存在");
                    return;
                }
                Bis_tool_gr bis_tool_gr = listGr.get(0);
                Bis_opeId bis_opeId = new Bis_opeId();
                bis_opeId.setOpe_ver(ret_prd_info.getNx_ope_ver_fk());
                bis_opeId.setOpe_id(ret_prd_info.getNx_ope_id_fk());
                Bis_ope bis_ope = bisOpeDao.get(bis_opeId);
                if (!bis_tool_gr.getId().getToolg_id().equals(bis_ope.getToolg_id_fk())) {
                    outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                    outTrx.setRtn_mesg("产品当前不在包装站点");
                    return;
                }
            }
            if (PROC_IN_PROCESS .equals(ret_prd_info.getPrd_stat())) {
                String hqlm = "from Bis_tool_gr where tool_id_fk = '" + toolIdFk + "'";
                List<Bis_tool_gr> listGr = bisToolGrDao.find(hqlm);
                if (listGr.size() == 0) {
                    outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                    outTrx.setRtn_mesg("设备代码[" + toolIdFk + "]群组信息不存在");
                    return;
                }
                Bis_tool_gr bis_tool_gr = listGr.get(0);
                Bis_opeId bis_opeId = new Bis_opeId();
                bis_opeId.setOpe_ver(ret_prd_info.getCr_ope_ver_fk());
                bis_opeId.setOpe_id(ret_prd_info.getCr_ope_id_fk());
                Bis_ope bis_ope = bisOpeDao.get(bis_opeId);
                if (!bis_tool_gr.getId().getToolg_id().equals(bis_ope.getToolg_id_fk())) {
                    outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                    outTrx.setRtn_mesg("产品当前不在包装站点");
                    return;
                }
            }
           /* if (!"".equals(wo_id_fk) && wo_id_fk != null) {
                if (!wo_id_fk.equals(ret_prd_info.getWo_id_fk())) {
                    outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                    outTrx.setRtn_mesg("产品不属于当前工单");
                    return;
                }
            }*/
            Ret_prd_pack ret_prd_pack = retPrdPackDao.get(prdSeqId);
            if (ret_prd_pack != null) {
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]已经包装");
                return;
            }
            //查看同工单这个产品同机台的这个等级是否已经建立箱子了
            String prdGrade = ret_prd_info.getPrd_grade();
            String woIdFk = ret_prd_info.getWo_id_fk();
            String hql = "From Ret_prd_pack where prd_grade ='" + prdGrade + "' and wo_id_fk ='" + woIdFk + "' and tool_id_fk ='" + toolIdFk + "'";
            List<Ret_prd_pack> ret_prd_packList = retPrdPackDao.find(hql);
            if (ret_prd_packList.size() > 0) {
                boxFlag = true;
            }
            List<FpppackclmOA> oaryList = new ArrayList<FpppackclmOA>();
            FpppackclmOA oary = new FpppackclmOA();
            oary.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
            oary.setPrd_grade(ret_prd_info.getPrd_grade());
            oary.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
            oary.setBay_id(bis_tool.getBay_id());
            oary.setFab_id_fk(bis_tool.getFab_id_fk());
            oary.setWo_id_fk(ret_prd_info.getWo_id_fk());
            oary.setLot_id(ret_prd_info.getLot_id());
            oary.setBox_flag(boxFlag);
            oary.setPrd_stat(ret_prd_info.getPrd_stat());
            oary.setNx_ope_id_fk(ret_prd_info.getNx_ope_id_fk());
            Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_prd_info.getMdl_id_fk());
            oary.setBox_cnt(String.valueOf(bis_mdl_def.getBox_cnt()));
            oaryList.add(oary);
            outTrx.setOary(oaryList);
            outTrx.setRtn_code(RETURN_CODE_OK);
            outTrx.setRtn_mesg(RETURN_MESG_OK);
            return;

    }

    public void addPrdPackInfo(FpppackclmI inTrx, FpppackclmO outTrx) {
        FpppackclmIA iary = inTrx.getIary().get(0);
        String prdSeqId = iary.getPrd_seq_id();
        String boxIdFk = iary.getBox_id_fk();
        String evtUsr = inTrx.getEvt_usr();
        Timestamp current_timestamp = Util.getCurrentTimestamp();
        Ret_prd_info ret_prd_info = retPrdInfoDao.get(prdSeqId);
        if (ret_prd_info == null) {
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]不存在");
            return;
        }
        Ret_prd_pack ret_prd_packInfo = retPrdPackDao.get(prdSeqId);
        if (ret_prd_packInfo != null) {
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]已经包装");
            return;
        }
        Ret_prd_pack ret_prd_pack = new Ret_prd_pack();
        ret_prd_pack.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
        ret_prd_pack.setBox_id_fk(boxIdFk);
        ret_prd_pack.setPrd_grade(ret_prd_info.getPrd_grade());
        ret_prd_pack.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
        ret_prd_pack.setTool_id_fk(ret_prd_info.getCr_tool_id_fk());
        ret_prd_pack.setWo_id_fk(ret_prd_info.getWo_id_fk());
        ret_prd_pack.setSlot_no(ret_prd_info.getSlot_no());
        ret_prd_pack.setProc_id_fk(ret_prd_info.getCr_proc_id_fk());
        ret_prd_pack.setEvt_usr(evtUsr);
        ret_prd_pack.setEvt_timestamp(current_timestamp);
        retPrdPackDao.save(ret_prd_pack);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
    }

    public void getBoxId(FpppackclmI inTrx, FpppackclmO outTrx) {

        FpppackclmIA iary = inTrx.getIary().get(0);
        String prdSeqId = iary.getPrd_seq_id();
        String timeCdoe = Util.getEvtTimeforVer();
        String evtUsr = inTrx.getEvt_usr();
        String code="";
        Ret_prd_info ret_prd_info = retPrdInfoDao.get(prdSeqId);
        String hql = "From Bis_mdl_grd where mdl_id_fk='*' and  prd_grade='" + ret_prd_info.getPrd_grade()+ "'";
        List<Bis_mdl_grd> bis_mdl_grds = bisMdlGrdDao.find(hql);
        if(bis_mdl_grds.size()>0){
            code=bis_mdl_grds.get(0).getGrade_prt();
        }else {
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("产品等级信息没有维护");
        }
        Timestamp current_timestamp = Util.getCurrentTimestamp();
        String boxIdFk = "N" + timeCdoe;
        Ret_crt_noId ret_crt_noId = new Ret_crt_noId();
        ret_crt_noId.setKey_id(boxIdFk);
        ret_crt_noId.setType("N");
        Ret_crt_no ret_crt_no = retCrtNoDao.get(ret_crt_noId);
        Ret_crt_no ret_crt_noNew = new Ret_crt_no();
        if (ret_crt_no == null) {
            boxIdFk = boxIdFk + code +"Z"+"001";
            ret_crt_noNew.setId(ret_crt_noId);
            ret_crt_noNew.setSerial_no("001");
            ret_crt_noNew.setEvt_usr(evtUsr);
            ret_crt_noNew.setEvt_timestamp(current_timestamp);
            retCrtNoDao.save(ret_crt_noNew);
        } else {
            String strA = ret_crt_no.getSerial_no();
            String strB = String.format("%0" + 3 + "d", Integer.parseInt(strA) + 1);
            boxIdFk = boxIdFk + code + "Z"+strB;
            ret_crt_no.setSerial_no(strB);
            ret_crt_no.setEvt_usr(evtUsr);
            ret_crt_no.setEvt_timestamp(current_timestamp);
            retCrtNoDao.update(ret_crt_no);
        }
        List<FpppackclmOA> oaryList = new ArrayList<FpppackclmOA>();
        FpppackclmOA oary = new FpppackclmOA();
        oary.setBox_id_fk(boxIdFk);
        oaryList.add(oary);
        outTrx.setOary(oaryList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
    }

    public void queryAllBoxFnc(FpppackclmI inTrx, FpppackclmO outTrx) {
        FpppackclmIA iary = inTrx.getIary().get(0);
        String toolIdFk = iary.getTool_id_fk();
        String hql = "select distinct box_id_fk From  Ret_prd_pack where tool_id_fk ='" + toolIdFk + "'";
        List<String> boxIdFkList = retBoxDao.find(hql);
        List<FpppackclmOA> oaryList = new ArrayList<>();
        for (String boxIdFk : boxIdFkList) {
            FpppackclmOA oary = new FpppackclmOA();
            String hqls = "From Ret_prd_pack where box_id_fk ='" + boxIdFk + "'";
            List<Ret_prd_pack> ret_prd_packs = retPrdPackDao.find(hqls);
            if (ret_prd_packs.size() > 0) {
                oary.setPrd_grade(ret_prd_packs.get(0).getPrd_grade());
                oary.setPrd_qty(ret_prd_packs.size());
                oary.setBox_id_fk(ret_prd_packs.get(0).getBox_id_fk());
                oary.setMdl_id_fk(ret_prd_packs.get(0).getMdl_id_fk());
                oary.setWo_id_fk(ret_prd_packs.get(0).getWo_id_fk());
                oaryList.add(oary);
            }
            Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_prd_packs.get(0).getMdl_id_fk());
            oary.setBox_cnt(String.valueOf(bis_mdl_def.getBox_cnt()));
        }
        outTrx.setOary(oaryList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
    }

    public void delByBoxFnc(FpppackclmI inTrx, FpppackclmO outTrx) {
        List<FpppackclmIA> iaryList = inTrx.getIary();
        List<FpppackclmOA> oaryList = new ArrayList<>();
        if (iaryList.size() > 0) {
            for (FpppackclmIA fpppackclmIA : iaryList) {
                String boxIdFk = fpppackclmIA.getBox_id_fk();
                String hql = " From Ret_prd_pack where box_id_fk ='" + boxIdFk + "'";
                List<Ret_prd_pack> list = retPrdPackDao.find(hql);
                for (Ret_prd_pack ret_prd_pack : list) {
                    FpppackclmOA oary = new FpppackclmOA();
                    oary.setPrd_seq_id(ret_prd_pack.getPrd_seq_id());
                    oaryList.add(oary);
                }
                retPrdPackDao.deleteAll(list);
            }
        }
        outTrx.setOary(oaryList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
    }

    public FpppackclmO errActionFnc() {
        FpppackclmO fpppackclmO = new FpppackclmO();
        fpppackclmO.setTrx_id(T_FPPUPKCLM);
        fpppackclmO.setType_id(TRX_OUT);
        fpppackclmO.setRtn_code(E_FPPUPKCLM_INVALID_INPUT + "");
        fpppackclmO.setRtn_mesg("Invalid Action_flg");
        return fpppackclmO;
    }



    public void makeLot(FpppackclmI inTrx, FpppackclmO outTrx) {

        Timestamp current_timestamp = Util.getCurrentTimestamp();
        List<FpppackclmIA> iaryList = inTrx.getIary();
        String evtUsr = inTrx.getEvt_usr();
        List<Ret_prd_info> ret_prd_infos = new ArrayList<Ret_prd_info>();
//        if(Util.isSpaceCheck(lotId)){
//            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
//            outTrx.setRtn_mesg("LOT代码不能为空");
//            return;
//        }
        String lotId = "";
        if (iaryList.size() > 0) {
            for (FpppackclmIA fpppackclmIA : iaryList) {
                Ret_prd_info prdInfo = retPrdInfoDao.get(fpppackclmIA.getPrd_seq_id());
                if (null == prdInfo) {
                    outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                    outTrx.setRtn_mesg("玻璃不存在");
                    return;
                }
                if (BANK_IN.equals(prdInfo.getBnk_flg())) {
                    outTrx.setRtn_code(E_FPPMOVEIN_TOOL_WO_NOT + "");
                    outTrx.setRtn_mesg("该屏体在在制仓内，无法入账");
                    return;
                }
                ret_prd_infos.add(prdInfo);

            }
            lotId = this.createlotId(inTrx);
//            String hql = "From Ret_prd_info where 1=1";
//            List<Ret_prd_info> allprds = retPrdInfoDao.find(hql);
//            List<Ret_prd_info> filterList = allprds.stream().filter(a -> a.getLot_id().equals(lotId)).collect(Collectors.toList());
//            if(filterList.size() > 0){
//                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
//                outTrx.setRtn_mesg("该批次号已经用过");
//                return;
//            }
            for (Ret_prd_info ret_prd_info :ret_prd_infos){
                ret_prd_info.setLot_id(lotId);
                ret_prd_info.setEvt_timestamp(current_timestamp);
                ret_prd_info.setEvt_usr(evtUsr);
                ret_prd_info.setEvt_cate("ALOT");
                ret_prd_info.setExt_14(lotId);
                retPrdInfoDao.update(ret_prd_info);
            }
        } else {
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("组批次玻璃数量为0");
            return;
        }
        outTrx.setLot_id(lotId);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    public void oneCancelLot(FpppackclmI inTrx, FpppackclmO outTrx) {

        Timestamp current_timestamp = Util.getCurrentTimestamp();
        List<FpppackclmIA> iaryList = inTrx.getIary();
        String evtUsr = inTrx.getEvt_usr();
        if (iaryList.size() > 0) {
            for (FpppackclmIA fpppackclmIA : iaryList) {
                Ret_prd_info prdInfo = retPrdInfoDao.get(fpppackclmIA.getPrd_seq_id());
                if (null == prdInfo) {
                    outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                    outTrx.setRtn_mesg("玻璃不存在");
                    return;
                }
                prdInfo.setLot_id("");
                prdInfo.setEvt_timestamp(current_timestamp);
                prdInfo.setEvt_usr(evtUsr);

                retPrdInfoDao.update(prdInfo);
            }
        } else {
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("请选择需要取消组批次的玻璃");
            return;
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    public void wholeCancelLot(FpppackclmI inTrx, FpppackclmO outTrx) {

        Timestamp current_timestamp = Util.getCurrentTimestamp();
        String evtUsr = inTrx.getEvt_usr();
        String lotId = inTrx.getLot_id();
        String sql = "FROM Ret_prd_info WHERE 1=1 and lot_id='" + lotId + "'";
        List<Ret_prd_info> ret_prd_infoList = retPrdInfoDao.find(sql);
        if (ret_prd_infoList.size() > 0) {
            for (Ret_prd_info ret_prd_info : ret_prd_infoList) {
                ret_prd_info.setLot_id("");
                ret_prd_info.setEvt_timestamp(current_timestamp);
                ret_prd_info.setEvt_usr(evtUsr);

                retPrdInfoDao.update(ret_prd_info);
            }
        } else {
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("此批次玻璃数量为0");
            return;
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    private String createlotId(FpppackclmI inTrx){

        String lot_id="";
        Ret_crt_no ret_crt_no = new Ret_crt_no();
        Ret_crt_noId id = new Ret_crt_noId();
        String key_id = this.getStringDate();
        id.setType(LOT_TYPE);
        id.setKey_id(key_id);
        ret_crt_no = retCrtNoDao.get(id);
        if(ret_crt_no == null){
            Ret_crt_no ret_crt_no_new = new Ret_crt_no();
            ret_crt_no_new.setId(id);
            String serial_no = String.format("%03d", 1);
            ret_crt_no_new.setSerial_no(serial_no);
            ret_crt_no_new.setEvt_usr(inTrx.getEvt_usr());
            ret_crt_no_new.setEvt_timestamp(Util.getCurrentTimestamp());
            lot_id = key_id + "001";
            retCrtNoDao.save(ret_crt_no_new);
        }else{
            String old_serial_no = ret_crt_no.getSerial_no();
            int old_serial_no_int = Integer.valueOf(old_serial_no);
            int new_serial_no_int = old_serial_no_int + 1;
            String new_serial_no = String.format("%03d", new_serial_no_int);
            ret_crt_no.setSerial_no(new_serial_no);
            ret_crt_no.setEvt_usr(inTrx.getEvt_usr());
            ret_crt_no.setEvt_timestamp(Util.getCurrentTimestamp());
            retCrtNoDao.update(ret_crt_no);
            lot_id = key_id + new_serial_no;
        }
        return lot_id;
    }
    public  String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
}
